package com.jbpm.servlet;



import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jbpm.api.Configuration;
import org.jbpm.api.Execution;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;
import org.jbpm.api.model.ActivityCoordinates;
import org.jbpm.api.task.Task;
/**
 * 请假流程Servlet
 * @author 周文滔 <zhouwentao16@gmail.com>
 * @time 2012-5-27 下午05:39:32
 */
public class LeaveServlet extends HttpServlet {
	private ProcessEngine processEngine = null;
	private RepositoryService repositoryService = null;
	private ExecutionService executionService = null;
	private ProcessInstance processInstance = null;
	private TaskService taskService = null;
	private Task task = null;
	private Execution execution = null;
	private String url = null;
	private List list = null;
	private List<Task> taskList = null;

	public LeaveServlet() {
		super();
	}

	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		// 获取流程引擎
		processEngine = new Configuration().buildProcessEngine();
		repositoryService = processEngine.getRepositoryService();
		executionService = processEngine.getExecutionService();
		taskService = processEngine.getTaskService();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		process(request, response);
	}

	protected void process(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		HttpSession session = request.getSession();
		if (action.equals("leave")) {
			request.getRequestDispatcher("").forward(request, response);
		} else if (action.equals("list")) {
			String username = (String) session.getAttribute("username");
			List<ProcessDefinition> pdList = repositoryService
					.createProcessDefinitionQuery().list();
			List<ProcessInstance> piList = executionService
					.createProcessInstanceQuery().list();
			List<Task> taskList = taskService.findPersonalTasks(username);
			session.setAttribute("pdList", pdList);
			session.setAttribute("piList", piList);
			session.setAttribute("taskList", taskList);
			request.getRequestDispatcher("list.jsp").forward(request, response);
		}else if (action.equals("start")) {
			Map map = new HashMap();
			map.put("user",session.getAttribute("username"));
			executionService.startProcessInstanceById(request.getParameter("id"),map);
			request.getRequestDispatcher("leave?action=list").forward(request, response);
		}else if (action.equals("submit")) {
			String taskId = request.getParameter("taskId");
			String user = request.getParameter("user");
			int day = Integer.parseInt(request.getParameter("day"));
			String reason = request.getParameter("reason");
			reason = new String(reason.getBytes("ISO-8859-1"),"UTF-8");
			Map map = new HashMap();
			map.put("days", day);
			map.put("reason", reason);
			taskService.completeTask(taskId, "to_teacher",map);
			
			request.getRequestDispatcher("leave?action=list").forward(request, response);
		}else if (action.equals("view")) {
			String id = request.getParameter("id");
			processInstance = executionService.findProcessInstanceById(id);
			Set<String> activityNames = processInstance.findActiveActivityNames();
			ActivityCoordinates ac = repositoryService.getActivityCoordinates(processInstance.getProcessDefinitionId(),activityNames.iterator().next());
			request.setAttribute("ac", ac);
			request.getRequestDispatcher("view.jsp").forward(request, response);
		}else if (action.equals("deploy")) {
			repositoryService.createDeployment().addResourceFromClasspath("leave.jpdl.xml").deploy();
		}else if(action.equals("sh")){
			String taskId = request.getParameter("taskId");
			String result = request.getParameter("result");
			result = new String(result.getBytes("ISO-8859-1"), "UTF-8");
			System.out.println(result);
			taskService.completeTask(taskId, result);
			request.getRequestDispatcher("leave?action=list").forward(request, response);
		}
	}

}
